#include <iostream>
#include <stdio.h>
#include <unistd.h>

#include <gtest/gtest.h>
#include <boost/thread.hpp>
#include <boost/progress.hpp>
#include <libgo/coroutine.h>

#define O(x) std::cout << x << std::endl

namespace tsleep {

    std::atomic_int g_value{ 0 };
    static const int co_count = 100;
    static const int switch_per_co = 10;
    static int thread_count = 1;

    void f1() {
        for (int i = 0; i < switch_per_co; ++i) {
            //printf("f1 %d\n", g_value++);
            g_value++;
            usleep(1000);
            co_yield;
        }
    }
}


TEST(sleep, test) {
    using namespace tsleep;

    auto thread_count = 10;
    for (int i = 0; i < co_count; ++i) {
        go f1;
    }

    {
        auto start = std::chrono::high_resolution_clock::now();
        co_sched.Start();
        auto end = std::chrono::high_resolution_clock::now();
        auto milli = std::chrono::duration_cast<std::chrono::milliseconds>(end - start).count();
        O(thread_count << " threads, run " << co_count << " coroutines, " << co_count * switch_per_co << " times switch. cost" << milli);
    }
}

